﻿using Dapper;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;

namespace FinanceAPI.Model.Dappers
{
    public class DapperHelper
    {
        public List<T> GetEntitys<T>(string sql, string sqlconn)
        {
            var entitys = new List<T>();
            using (IDbConnection conn = GetSqlConnection(sqlconn))
            {
                string sqlCommandText = sql;
                entitys = conn.Query<T>(sqlCommandText).ToList();
                conn.Close();
            }
            
            return entitys;
        }

        public int Execute(string executeSql, string sqlconn)
        {
            using (IDbConnection conn = GetSqlConnection(sqlconn))
            {
                return conn.Execute(executeSql);
            }
        }

        public SqlConnection GetSqlConnection(string sqlConnectionString)
        {
            SqlConnection conn = new SqlConnection(sqlConnectionString);
            conn.Open();
            return conn;
        }
    }
}
